package com.ham.mud.commands;

import com.ham.mud.ServerConnection;
import com.ham.mud.characters.mobs.Mob;
import com.ham.mud.characters.npcs.QuestMasterNPC;
import com.ham.mud.characters.player.Player;
import com.ham.mud.quests.FindQuest;
import com.ham.mud.quests.KillQuest;
import com.ham.mud.quests.Quest;
import com.ham.mud.quests.QuestService;
import com.ham.mud.rooms.Room;
import com.ham.mud.rooms.RoomService;

import java.util.List;

/**
 * Created by hlucas on Jul 19, 2011 at 10:26:43 AM
 */
public class QuestCommand extends PlayerCommand {
    @Override
    public void execute(ServerConnection connection, String[] params) {
        if(params.length == 0) {
            questWhat(connection);
            return;
        }

        Player player = connection.getPlayer();
        String param = params[0];
        if("request".equals(param)) {
            questRequest(connection, player);
        } else if("complete".equals(param)) {
            questComplete(connection, player);
        } else if("info".equals(param)) {
            questInfo(connection, player);
        } else {
            questWhat(connection);
        }
    }

    private void questRequest(ServerConnection connection, Player player) {
        Quest currentQuest = player.getQuest();
        if(currentQuest != null) {
            connection.printSolo("You are already on a quest!");
            return;
        }

        QuestMasterNPC questMaster = getQuestMaster(RoomService.getRoom(connection));
        if(questMaster == null) {
            connection.printSolo("You need a questmaster to do that.");
            return;
        }

        connection.printStart("You request a quest from " + questMaster.getName() + ".");
        Quest quest = QuestService.newQuest(player);
        player.setQuest(quest);
        quest.display(connection);
        connection.printEnd();
    }

    private void questComplete(ServerConnection connection, Player player) {
        Quest quest = player.getQuest();
        if(quest == null) {
            connection.printSolo("You must be on a quest to complete a quest.");
            return;
        }

        if(!quest.isComplete(player)) {
            connection.printStart("You have not completed the quest.");
            quest.display(connection);
            connection.printSolo("");
            return;
        }

        if(quest instanceof FindQuest) {
            player.removeItem(((FindQuest) quest).getItem());
        }

        int qp = quest.getRewardPoints();
        int gp = quest.getRewardGold();

        player.incrementQuestPoints(qp);
        player.incrementGold(gp);

        connection.printStart("You complete the quest.");
        connection.println("    You earn " + qp + " quest points!");
        connection.println("    You earn " + gp + " gold!");
        connection.printEnd();
        player.setQuest(null);
    }

    private void questInfo(ServerConnection connection, Player player) {
        Quest quest = player.getQuest();
        if(quest == null) {
            connection.printSolo("You are not on a quest.");
            return;
        }

        if(quest.isComplete(player)) {
            if(quest instanceof KillQuest) {
                connection.printSolo("You have already slain what you set out to kill.");
            } else if(quest instanceof FindQuest) {
                connection.printSolo("You have found the item you set out to find.");
            } else {
                connection.printSolo("You have already completed your quest.");
            }
            return;
        }

        connection.println();
        quest.display(connection);
        connection.printEnd();
    }

    private void questWhat(ServerConnection connection) {
        connection.printSolo("Quest: list, buy, sell, request, time, info, complete");
    }

    private QuestMasterNPC getQuestMaster(Room room) {
        List<Mob> mobs = room.getMobs();
        for(Mob mob : mobs) {
            if(mob instanceof QuestMasterNPC) {
                return (QuestMasterNPC) mob;
            }
        }
        return null;
    }

    @Override
    protected String getCommandName() {
        return "questt";
    }
}
